﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT_Nhom1A.BUS;
using TMDT_Nhom1A.DTO;

namespace TMDT_Nhom1A.San_Pham
{
    public partial class ChiTietSanPham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["ElectricDeviceID"] != null)
                {
                    dldetailproduct.DataSource = ElectricDevice_BUS.DetailElectricDevice(Request.QueryString["ElectricDeviceID"]);
                    dldetailproduct.DataBind();
                }
                if (Request.QueryString["CouponID"] != null)
                {
                    dldetailproduct.DataSource = ElectricDevice_BUS.DetailElectricDeviceByCouponID(Request.QueryString["CouponID"]);
                    dldetailproduct.DataBind();
                }
            }
        }

        protected void ibBack_Click(object sender, ImageClickEventArgs e)
        {
            string url = "../Default.aspx";
            if(Request.QueryString["ParentCatalogueID"] != null)
            {
                url = "DanhSachSanPham.aspx" + "?ParentCatalogueID=" + Request.QueryString["ParentCatalogueID"] + "&Name=" + Request.QueryString["Name"].ToString();
            }
            if (Request.QueryString["ChildCatalogueID"] != null)
            {
                url = "DanhSachSanPham.aspx" + "?ChildCatalogueID=" + Request.QueryString["ChildCatalogueID"] + "&Name=" + Request.QueryString["Name"].ToString();
            }
            
            Response.Redirect(url);
        }

        protected void dldetailproduct_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "AddToCart")
            {
                TextBox tbquantity = (TextBox)e.Item.FindControl("tbquantity");
                if (tbquantity.Text != "")
                {
                    MyOrderProduct_Online ItemCart = new MyOrderProduct_Online();
                    Label lbElectricDeviceID = (Label)e.Item.FindControl("lbElectricDeviceID");
                    ItemCart.ElectricDeviceID = lbElectricDeviceID.Text;
                    Label lbRetailPrice = (Label)e.Item.FindControl("lbRetailPrice");
                    ItemCart.RetailPrice = decimal.Parse(lbRetailPrice.Text.ToString());
                    Label lbDealerPrice = (Label)e.Item.FindControl("lbDealerPrice");
                    ItemCart.DealerPrice = decimal.Parse(lbDealerPrice.Text.ToString());
                    Label lbNonDiscountPrice = (Label)e.Item.FindControl("lbNonDiscountPrice");
                    if (lbNonDiscountPrice.Text != "")
                        ItemCart.PriceCoupon = decimal.Parse(lbNonDiscountPrice.Text.ToString());

                    ItemCart.Quantity = int.Parse(tbquantity.Text);
                    Cart cart = (Cart)Session["Cart"];
                    cart.AddToCart(ItemCart);
                    Session["TotalPrice"] = cart.CalulateTotalPrice();

                    tbquantity.Text = "";
                }
            }
        }

        protected void dldetailproduct_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                string NonDiscountPrice = "";
                string DiscountPrice = "";
                Label lbElectricDeviceID = (Label)e.Item.FindControl("lbElectricDeviceID");
                Coupon_BUS.GetPriceCouponOfElectricDevice(lbElectricDeviceID.Text, ref NonDiscountPrice, ref DiscountPrice);
                if (NonDiscountPrice != "" && DiscountPrice != "")
                {
                    Label lbDiscountPrice = (Label)e.Item.FindControl("lbDiscountPrice");
                    Label lbNonDiscountPrice = (Label)e.Item.FindControl("lbNonDiscountPrice");
                    lbDiscountPrice.Text = DiscountPrice;
                    lbNonDiscountPrice.Text = NonDiscountPrice;
                    e.Item.FindControl("divdel").Visible = true;
                    e.Item.FindControl("divnondel").Visible = false;
                }
                if (NonDiscountPrice == "" && DiscountPrice == "")
                {
                    e.Item.FindControl("divdel").Visible = false;
                    e.Item.FindControl("divnondel").Visible = true;
                }
            }
        }
    }
}
